#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys,urllib,re,md5,os
import MySQLdb
import ConfigParser
import htmllib
import formatter
import string
import time
import thread
import re
import urllib2
import socket
import datetime
class getimg:
    
    def __init__(self):
        #self.picDir = os.path.dirname(__file__)
        self.picDir = "/opt/img/"
        self.beginTime = time.time()
    
    def mkdir(self,ndate):
        #ndate=datetime.datetime.now()
        # ndate="2008-10-28 17:46:39"
        if not os.path.exists('%s%s' %(self.picDir,ndate["year"])):
            os.mkdir('%s%s' %(self.picDir,ndate["year"]))
        if not os.path.exists('%s%s/%s' % (self.picDir,ndate["year"],ndate["month"])):
            os.mkdir('%s%s/%s' % (self.picDir,ndate["year"],ndate["month"]))
        if not os.path.exists('%s%s/%s/%s' % (self.picDir,ndate["year"],ndate["month"],ndate["day"])):
            os.mkdir('%s%s/%s/%s' % (self.picDir,ndate["year"],ndate["month"],ndate["day"]))
        #if not os.path.exists('%s%s/%s/%s/%s' % (self.picDir,ndate["year"],ndate["month"],ndate["day"],obj)):
            #os.mkdir('%s%s/%s/%s/%s' % (self.picDir,ndate["year"],ndate["month"],ndate["day"],'small'))
            #os.mkdir('%s%s/%s/%s/%s' % (self.picDir,ndate["year"],ndate["month"],ndate["day"],'mark'))
        #   os.mkdir('%s%s/%s/%s/%s' % (self.picDir,ndate["year"],ndate["month"],ndate["day"],obj))
        dir = str("%s/%s/%s/%s" %(ndate["year"],ndate["month"],ndate["day"],""))    
        return dir
    
    def isHtml(self,url):
        eventNum = 3
        for e in range(0,eventNum):
            html = self.getHtml(url)
            if len(html) > 0:
                return html
            if e > eventNum:
                return 0
            print "重试%d" % e
            time.sleep(1)

        
    def getImgUrl(self,url):
        html = self.isHtml(url)
        try:
            ndate = html.split("时间：")[1].split("<br />")[0]
        except:
            ndate = ""
            
        try:
            dlist = ndate.split(" ")[0].split("-")
            year=dlist[0]
            month=dlist[1]
            day=dlist[2]
        except:
            year=""
            month=""
            day=""
            
        try:
            pic_url = html.split("var pic_url = '")[1].split("';")[0]
        except:
            pic_url = ""
            
        try:
            pic_title = html.split("var pic_title = '")[1].split("';")[0]
        except:
            pic_title = ""
        
        ndateDict = {
            "year":year,
            "month":month,
            "day":day,
            "pic_url":pic_url,
            "pic_title":pic_title
        }
        return ndateDict
    
    def download(self,ndateDict,threadNum,root):
        image = ndateDict["pic_url"]
        dir = self.mkdir(ndateDict)
        newImg = dir+image.split('/')[-1]
        caption = ndateDict["pic_title"]
        self.insert(newImg,image,root,caption)
        thread.start_new_thread(self.downloadThread,(threadNum,image,self.picDir+dir))
    
    def insert(self,url,old_url,root,caption):
        limitSql = "insert into photo (uid,url,old_url,root,caption) values (0,'"+url+"','"+old_url+"','"+root+"','"+caption+"')"
        db = self.dbConn()
        cursor = db.cursor()
        cursor.execute("SET NAMES 'utf8'")
        cursor.execute(limitSql)
        db.close()
    
    def downloadThread(self,threadNum,url,dir):
        try:
            urllib.urlretrieve(url,dir+url.split('/')[-1])
            print threadNum," thread"
        except:
            print "error download"
    
    def se(self):
        limitSql = "select * from img_list where status=0 limit 10"
        db = self.dbConn()
        cursor = db.cursor()
        cursor.execute(limitSql)
        info = cursor.fetchall()
        db.close()
        return info
    
    def up(self,id):
        sql = "update img_list set status=-1 where id=%s" % (id)
        db = self.dbConn()
        cursor = db.cursor()
        cursor.execute(sql)
        db.close()
            
    def mark(self,lists):
        db = self.dbConn()
        for row in lists:
            sql = "update img_list set status=1 where id=%d" % (row[0])
            cursor = db.cursor()
            cursor.execute(sql)
        db.close()
        
    #get file code
    def getHtml(self,domain):
        try:
            wp = urllib.urlopen(domain)
        except:
            print 'url exception'
        content = wp.read()
        wp.close()
        return content
    
    #get config file
    def initConfig(self,configFile):
        config = ConfigParser.ConfigParser();
        config.readfp(open(configFile));
        return config;
    
    def matchCode(self,regular,content):
        str = re.compile(regular,re.I|re.S)
        return str.findall(content)
    
    def dbConn(self,dbServer="default"):
        config = self.initConfig("../config/_db.ini")
        user = str(config.get(dbServer,"user"))
        db   = str(config.get(dbServer,"db"))
        passwd   = str(config.get(dbServer,"passwd"))
        host   = str(config.get(dbServer,"host"))
        conn = MySQLdb.connect(user=user, db=db, passwd=passwd, host=host)
        return conn
    
    def run(self):
        while True:
            list = g.se()
            g.mark(list)
            if len(list) < 1:
              break
            threadNum = 1
            for k in list:
                url = "http://album.360quan.com/u/"+k[1]
                self.up(k[0])
                code = g.getImgUrl(url)
                g.download(code,threadNum,k[1])
                threadNum = threadNum +1
                print "当前 %s -> %s " % (k[0],k[1]) ,time.time() - self.beginTime

if __name__=='__main__':
    g = getimg()
    g.run()